package com.ge.med.cchelper.listener;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;

import com.ge.med.cchelper.bean.CSession;
import com.ge.med.cchelper.util.LogUtil;
import com.ge.med.cchelper.util.PropHelper;
import com.ge.med.cchelper.util.ReourceHelper;

public class DetailVobListener implements Listener {

	String vobsName;
	List<String> selectDirList;
	Text[] textDir;
	Button[] buttonDir;

	public DetailVobListener(Text[] textDir,Button[] buttonDir) {
           this.textDir = textDir;
           this.buttonDir = buttonDir;
	}

	public void handleEvent(Event arg0) {

		List<String> selectDirList = new ArrayList<String>();
		
		for(int i=0;i<buttonDir.length;i++){
			if(buttonDir[i].getSelection()){
				selectDirList.add(textDir[i].getText());
			}
		}
		
		IProject currentProject =ReourceHelper.getProject();
		ReourceHelper resource = ReourceHelper.getResource();
		CSession cs=resource.getSession(currentProject.getName());
		cs.setVobWorkDirs(selectDirList);

		String confdir =  currentProject.getLocation().toOSString();
		String confFile = confdir+File.separator+PropHelper.CONFIG_FILE;
		File file =new File(confFile);
		if(!file.exists()){
			try {
				file.createNewFile();
			} catch (IOException e1) {
				LogUtil.err("create conf file fail.");
			}
		}
		String downDir = selectDirList.toString().substring(1,selectDirList.toString().length()-1);
		PropHelper.writeProperties(confFile, PropHelper.VOB_DOWNLOAD,downDir);
	}
	
	
}
